package com.wkg.lotterysystem.service.activitystatus.operater;

import com.wkg.lotterysystem.dao.dataobject.ActivityPrizeDo;
import com.wkg.lotterysystem.dao.mapper.ActivityPrizeMapper;
import com.wkg.lotterysystem.service.dto.ConvertActivityStatusDTO;
import com.wkg.lotterysystem.service.enums.ActivityPrizeStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class PrizeOperator extends AbstractActivityOperator {
    @Autowired
    private ActivityPrizeMapper activityPrizeMapper;

    @Override
    public Integer sequence() {
        return 1;
    }

    @Override
    public Boolean needConvert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        Integer activityId = convertActivityStatusDTO.getActivityId();
        Integer prizeId = convertActivityStatusDTO.getPrizeId();
        ActivityPrizeStatusEnum targetPrizeStatus = convertActivityStatusDTO.getTargetPrizeStatus();
        if (null == prizeId
                || null == targetPrizeStatus
                || null == activityId) {
            return false;
        }
        ActivityPrizeDo activityPrizeDo = activityPrizeMapper.selectByAPId(prizeId, activityId);
        if (null == activityPrizeDo) {
            return false;
        }

        //判断 当前奖品状态是否和目标状态一致,一致返回false
        if (targetPrizeStatus.name().equalsIgnoreCase(activityPrizeDo.getStatus())) {
            return false;
        }
        return true;
    }

    @Override
    public Boolean convert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        Integer activityId = convertActivityStatusDTO.getActivityId();
        Integer prizeId = convertActivityStatusDTO.getPrizeId();
        ActivityPrizeStatusEnum targetPrizeStatus = convertActivityStatusDTO.getTargetPrizeStatus();
        try {
            activityPrizeMapper.updateStatus(activityId, prizeId, targetPrizeStatus.name());
            return true;
        } catch (Exception e) {
            return false;
        }

    }
}
